
#include <vfs.h>
#include <gfx/ieffect.h>
#include <gfx/itexture.h>
#include <gfx/material.h>
#include <manager/materialmanager.h>
#include <assert.h>

ceMaterialManager* ceMaterialManager::static_instance = 0;

ceMaterialManager::ceMaterialManager()
	: _defaultMaterial(0)
{
  CE_OBJECT_CONSTR
}

ceMaterialManager::~ceMaterialManager()
{
  // TODO: Implement ceMaterialManager destructor
}

ceMaterialManager* ceMaterialManager::Get()
{
	if (!ceMaterialManager::static_instance)
		{
			ceMaterialManager::static_instance = new ceMaterialManager();
			ceMaterialManager::static_instance->AddRef();
		}

	return ceMaterialManager::static_instance;
}


ceMaterialManager* ceMaterialManager::Reset(ceMaterialManager *materialManager)
{
	if (!materialManager)
		{
			materialManager = new ceMaterialManager();
		}

  CE_SET(ceMaterialManager::static_instance, materialManager);
	return ceMaterialManager::static_instance;
}


void ceMaterialManager::AddEffect(const std::string &name, iEffect *effect)
{
  assert (effect);
  CE_SET(_effects[name], effect);
}

iEffect* ceMaterialManager::GetEffect (const std::string &filename)
{
	std::map<std::string, iEffect*>::iterator it;
	it = _effects.find(filename);
  if (it != _effects.end ())
    {
      return it->second;
    }

  return 0;
}


const iEffect *ceMaterialManager::GetEffect(const std::string &name) const
{
	std::map<std::string, iEffect*>::const_iterator it;
  it = _effects.find(name);
  if (it != _effects.end ())
    {
      return it->second;
    }
  return 0;
}

std::list<std::string> ceMaterialManager::GetEffectNames() const
{
  std::list<std::string> names;
  std::map<std::string, iEffect*>::const_iterator it;
  for (it=_effects.begin(); it!=_effects.end(); ++it)
    {
      names.push_back(it->first);
    }
  return names;
}


void ceMaterialManager::AddMaterial(const std::string &name, ceMaterial *material, bool isDefault)
{
	assert (material);
  CE_SET(_materials[name], material);

	if (isDefault)
		{
      CE_SET(_defaultMaterial, material);
		}
}

ceMaterial* ceMaterialManager::GetMaterial(const std::string &name)
{
	std::map<std::string, ceMaterial*>::iterator it;
	it = _materials.find(name);
	if (it != _materials.end ())
		{
			return it->second;
		}

  return 0;
}

const ceMaterial* ceMaterialManager::GetMaterial(const std::string &name) const
{
  std::map<std::string, ceMaterial*>::const_iterator it;
  it = _materials.find(name);
  if (it != _materials.end ())
    {
      return it->second;
    }
  return _defaultMaterial;
}

std::list<std::string> ceMaterialManager::GetMaterialNames() const
{
  std::list<std::string> names;
  std::map<std::string, ceMaterial*>::const_iterator it;
  for (it=_materials.begin(); it!=_materials.end(); ++it)
    {
      names.push_back(it->first);
    }
  return names;
}


void ceMaterialManager::SetDefaultMaterial(ceMaterial *material)
{
  CE_SET(_defaultMaterial, material);
}

ceMaterial* ceMaterialManager::GetDefaultMaterial()
{
  return _defaultMaterial;
}

const ceMaterial* ceMaterialManager::GetDefaultMaterial() const
{
	return _defaultMaterial;
}


void ceMaterialManager::AddTexture(const std::string &name, iTexture *texture)
{
  assert (texture);
  CE_SET(_textures[name], texture);
}

iTexture* ceMaterialManager::GetTexture(const std::string &name)
{
  std::map<std::string, iTexture*>::iterator it;
  it = _textures.find(name);
  if (it != _textures.end ())
    {
      return it->second;
    }
  return 0;
}


const iTexture* ceMaterialManager::GetTexture(const std::string &name) const
{
  std::map<std::string, iTexture*>::const_iterator it;
  it = _textures.find(name);
  if (it != _textures.end ())
    {
      return it->second;
    }


  return 0;
}


std::list<std::string> ceMaterialManager::GetTextureNames() const
{
  std::list<std::string> names;
  std::map<std::string, iTexture*>::const_iterator it;
  for (it=_textures.begin(); it!=_textures.end(); ++it)
    {
      names.push_back(it->first);
    }
  return names;
}


